package com.sync.syncmasterdata.customer.pojo;


import com.sync.syncmasterdata.customer.pojo.oa.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import java.io.StringWriter;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 请求 OA xml对象 (oa产品对象 转 推送xml产品对象)
 * @author kent
 */
@Slf4j
@Component
public class RequestOACustomer {

    @Autowired
    private CustomerOaRoot root;
    @Autowired
    private CustomerOaMaintable maintable;
    @Autowired
    private CustomerOaData data;
    @Autowired
    private CustomerOaRootHeader header;


    private Map<Class<?>,String> typeMap;

    public RequestOACustomer(){
        this.typeMap=new HashMap<>();
        typeMap.put(String.class,"varchar(599)");
        typeMap.put(int.class,"int");

    }

    /**
     * 将oa对象转换为xml请求对象
     */
    public String convertRequestObj(OACustomer oa, String headerId){
        List<CustomerOaField> CustomerOaFields=new ArrayList<>();

        CustomerOaField inventoryForOaField=null;
        Field[] oaFields=oa.getClass().getDeclaredFields();
        for(Field f:oaFields){

            inventoryForOaField=new CustomerOaField();
            inventoryForOaField.setFiledname(f.getName());
            inventoryForOaField.setFiledlabel(f.getName());
            inventoryForOaField.setFileddbtype(getFieldTypeByName(f.getType()));
            inventoryForOaField.setFiledvalue(getFieldValueByName(f.getName(),oa)!=null?getFieldValueByName(f.getName(),oa).toString():null);
            inventoryForOaField.setFieldshowname(getFieldValueByName(f.getName(),oa)!=null?getFieldValueByName(f.getName(),oa).toString():null);
            CustomerOaFields.add(inventoryForOaField);
        }

        //封装oa-xml对象
        maintable.setFields(CustomerOaFields);
        data.setMaintable(maintable);
        root.setData(data);
        header.setId(headerId);
        root.setHeader(header);

        //转xml
        StringWriter sw=new StringWriter();
        try{
            JAXBContext context = JAXBContext.newInstance(root.getClass());
            Marshaller marshaller=context.createMarshaller();
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,Boolean.TRUE);
            marshaller.marshal(root,sw);
        }catch(JAXBException ex){
            log.error(ex.toString());
        }
        //需替换
        
//        log.info("转换的对象为:{}",sw.toString());
        log.info("转入oa客户对象成功xml:{}",sw.toString());
        return sw.toString();
    }

    /**
     * 获取类属性的值
     * @param fieldName
     * @param o
     * @return
     */
    private Object getFieldValueByName(String fieldName, Object o) {
        try {
            String firstLetter = fieldName.substring(0, 1).toUpperCase();
            String getter = "get" + firstLetter + fieldName.substring(1);
            Method method = o.getClass().getMethod(getter, new Class[] {});
            Object value = method.invoke(o, new Object[] {});
            return value;
        } catch (Exception e) {
            System.out.println(e.toString());
            return null;
        }
    }

    private String getFieldTypeByName(Class<?> clazz){
        if(this.typeMap.containsKey(clazz)){
            //包含返回对应值
            return this.typeMap.get(clazz);
        }else{
            //否者统一返回
            return "varchar(200)";
        }
    }
}
